<?php
/**
 * ImpSoft Application Bootstrap
 * This is where all things in the application are initialized.
 * 
 * @package ImpSoft
 * @subpackage ImpSoft Bootstrap
 * @copyright Luke Visinoni (luke.visinoni@gmail.com)
 * @author Luke Visinoni (luke.visinoni@gmail.com)
 * @license GNU Lesser General Public License	
 * @todo I need to find a way to bootstrap each module. There is going to be code that I only
 *       want in one module or another, and that's how I want to do it.
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	/**
	 * Returns the front controller
	 */
	protected function getFrontController() {
	
		return Zend_Controller_Front::getInstance();
	
	}
	/**
	 * Initialize the request
	 */
    protected function _initRequest(array $options = array()) {
	
		/*
        // Ensure front controller instance is present, and fetch it
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');
		
        // Initialize the request object
        $request = new Zend_Controller_Request_Http();
        //$request->setBaseUrl('/foo');
		
        // Add it to the front controller
        $front->setRequest($request);
		
		echo "cool";
		
        // Bootstrap will store this value in the 'request' key of its container
        return $request;
		*/
	
    }
	/**
	 * Initialize Database Adapter
	 */
	protected function _initDb() {
	
		$resource = $this->getPluginResource('db');
		$db = $resource->getDbAdapter();
		$db->getProfiler()->setEnabled(true);
		Zend_Db_Table_Abstract::setDefaultAdapter($db);
		return $db;
	
	}
	/**
	 * Initialize Functions
	 */
	protected function _initFunctions() {
	
		// load up fluidics functions (convenience functions)
		Zend_Loader::loadFile('functions/fluidics.php');
		// load up mc2 convenience functions
		Zend_Loader::loadFile('functions/functions.php');
	
	}
	/**
	 * Initialize Autoloader
	 */
    protected function _initAutoload() {
	
		require_once 'Zend/Loader.php';
		$autoloader = with(Zend_Loader_Autoloader::getInstance())
			->registerNamespace('ImpSoft_')
			->registerNamespace('Q_')
			->setFallbackAutoloader(true);
		return $autoloader;
    
	} 
	/**
	 * Initialize Router
	 * 
	 * Base application routes should be stored in a file called "routes.php"
	 * in the config directory. 
	 */
	protected function _initRouter() {
	
		$front = $this->getFrontController();
		$router = $front->getRouter();
		$router->removeDefaultRoutes();
		$router->addRoutes(require APPLICATION_PATH . '/config/routes.php');
		return $router;
	
	}
	/**
	 * Initialize Session
	 */
	protected function _initSession() {
	
		$session = new Zend_Session_Namespace('ImpSoftCommerce');
		return $session;
	
	}
	/**
	 * Initialize the access control
	 */
	protected function _initAcl() {
	
		/** Creating Roles */
		$front = $this->getFrontController();
		try {
		    require APPLICATION_PATH . '/config/acl.php'; // $myAcl created in here
		    $role = 'guest';
		    if ($user = Zend_Auth::getInstance()->getIdentity()) $role = $user->user_role;
		    $aclPlugin = new Zend_Controller_Plugin_Acl($myAcl, $role);
		    //$front->setParam("acl", $aclPlugin);
		    $front->registerPlugin($aclPlugin);
		} catch (Zend_Acl_Exception $e) {
		    exit('Unable to set up permissions: '. $e->getMessage());
		}
		
		// acl helper (permissions system)
		Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_Acl());
		return $aclPlugin;
	
	}
	/**
	 * Initialize the view
	 */
    protected function _initView() {
	
		// @todo Some of this should be in the config file as a resource
        // Initialize view
		Zend_Layout::startMvc(array(
		    'layout'     => 'layouts/main',
		));
		
		$view = new ImpSoft_View;
		$this->view = $view;
		$view->doctype('HTML4_STRICT');
		
		// @todo this should come from the ini file or from admin panel
		// config settings (it should be customizable)
        $viewRenderer = with(Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'))
					->setView($view)
					//->setViewBasePathSpec(APPLICATION_PATH . 'view/templates')
					->setViewScriptPathSpec(':controller/:action.:suffix')
					->setViewScriptPathNoControllerSpec(':action.:suffix');
		
		// Add any additional helper paths we may be using
		// @todo I have to add the default view script path here so that the admin (and other modules)
		//       can default to it, but I would like to not have to
		$view->addHelperPath(APPLICATION_PATH . "/default/views/helpers", "ImpSoft_View_Helper")
			 ->addHelperPath(realpath(APPLICATION_PATH . "/../lib/ImpSoft/View/Helper"), "ImpSoft_View_Helper")
			 ->addScriptPath(APPLICATION_PATH . "/default/views/scripts");
		
		// set some view defaults
		// @todo need to figure out how to access config, other than creating o new instance manually
		// @todo Actually this should come from the database, not the config file
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', APPLICATION_ENV);
		// @todo Should the view have access to the entire config file? Probably not. When I implement
		//       the debug plugin, it will create a subset of the config to send to the view, as well as
		//       the db profiler and other useful things, but for now I'll just hack it like this
		$view->config = $config;
		$resource = $this->getPluginResource('db');
		$db = $resource->getDbAdapter();
		$view->dbprofiler = $db->getProfiler();
		$view->headTitle($config->resources->view->title);
		
		/**
		 * Set the defaults for the pagination, eventually I'll probably want to do this on a per-module
		 * basis. Who knows though.
		 */
		Zend_Paginator::setDefaultScrollingStyle('Sliding');
		Zend_View_Helper_PaginationControl::setDefaultViewPartial("includes/pagination.default.phtml");
		// is this needed? seems odd..
		// $paginator->setView($view);
		
        // Return it, so that it can be stored by the bootstrap
        return $view;
    
	}
	/**
	 * Initialize Controller Helpers
	 */
	protected function _initHelpers() {

		// @todo need to figure out how to access config, other than creating o new instance manually
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', APPLICATION_ENV);
		// remove default flash messenger and use my own
		Zend_Loader::loadFile("ImpSoft/Controller/Action/Helper/FlashMessenger.php");
		Zend_Controller_Action_HelperBroker::removeHelper('flashMessengar');
		Zend_Controller_Action_HelperBroker::addHelper(new ImpSoft_Controller_Action_Helper_FlashMessenger());
		// debug helper
		$options = array('debugLevel' => $config->debug->level);
		// @todo I need to find a better way to access the view
		$db = $this->getPluginResource('db')->getDbAdapter();
		//Zend_Controller_Action_HelperBroker::addHelper(new ImpSoft_Controller_Action_Helper_Debug($options, $this->view, $db));
		return true;

	}

}